
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
<script>
function binarysearch(arr,low,high,key){
    if(low > high) {
		return -1;
	}
	var mid = parseInt((high + low) / 2);
	if(arr[mid] == key) {
		return mid;
	} else if(arr[mid] > key) {
		high =mid -1;
		return binarysearch(arr, low, high, key);
	} else if(arr[mid] < key) {
		low = mid +1;
		return binarysearch(arr, low, high, key);
	}

}
function Binarysearch()
{ 
    var arr = [1,2,3,4,5,6,7,8,9];
    var i=binarysearch(arr,0,arr.length-1,4)+1;
    document.getElementById("demo").innerHTML="4 在这个数组的第"+i+"个位置";
    
}
</script>
</head>


<body>
<p>输入实例：1,2,3,4,5,6,7,8,9</p>
<p>要查找的是：4</p>





<button type="button" onclick="Binarysearch()">折半查找</button>
<p id="demo"></p>


</body>
</html>